<?php

require_once('../../config/maptrick.conf.php');
require_once(SITE_ROOT . 'lib/PHT.php');
require_once(SITE_ROOT . 'class/bean/Country.class.php');
require_once(SITE_ROOT . 'class/db/DBCountry.class.php');
require_once(SITE_ROOT . 'class/db/DBHCountry.class.php');

try {
	$HT = new CHPPConnection(VERSION, CHPPID, CHPPKEY, SITE_ROOT . 'tmp/hattrick/');
	if ($HT->connectUser(HTUSER, HTSECCODE)) {
		$leagues = $HT->getWorldDetails();
		$countries = new ArrayObject();
		for ($i=1; $i<=$leagues->getTotalLeague(); $i++) {
			$league = $leagues->getLeague($i);
			$country = new Country();
			$country->setCountryID($league->getId());
			$country->setName($league->getName());
			$country->setEnglishName($league->getEnglishName());
			$country->setDivisions($league->getNumberLevel());
			$country->setSeasons($league->getSeasonNumber());
			$country->setActiveUsers($league->getNumberActiveUsers());
			$country->setWaitingUsers($league->getNumberWaitingUsers());
			$country->setDate(time());
			$countries->append($country);
		}
		$HT->disconnect();
		$db = new DBCountry();
		$old_countries = $db->getCountries();
		$dbh = new DBHCountry();
		$dbh->saveCountries($old_countries);
		$db->saveCountries($countries);
		$db->updateCoolness($countries);
		echo date('Y-m-d', time()) . ' -  daily update successfull';
	}
	else echo "Login error !";
}
catch (Exception $e) {
	echo $e->getMessage();
}

?>